var express = require('express');
var router = express.Router();
var path = require('path');
var sqlite3 = require('sqlite3').verbose();
/* GET users listing. */
router.get('/', function (req, res, next) {
  var db = new sqlite3.Database(path.join(__dirname, "../db/data.s3db"));
  try {
    db.serialize(function () {
      var reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if (reg.test(req.query.username))
        sqlStr = "SELECT * from user where phone=='" + req.query.username + "'";
      else
        sqlStr = "SELECT * from user where username=='" + req.query.username + "'";
      // alert(req.query.username);
      db.all(sqlStr, function (err, rows) {
        if (rows.length == 0)
          res.send('0'); //用户不存在
        else if (req.query.password != rows[0].password)
          res.send('2'); //密码错误
        else {
          res.send('1'); //登录成功
        }
      });
    });
  } catch (ex) {
    console.log(ex);
  }
});

module.exports = router;